﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing;

public partial class Admin_Menu : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Session["username"].ToString().Length == 0)
                Response.Redirect("login.aspx");
            if (Session["permission"].ToString()=="1")
                Response.Redirect("default.aspx");
            nhapMenuCha();
            nhapMenuCon();
        }
    }
    protected void nhapMenuCha()
    {
        clsTableAction clsTable = new clsTableAction("Menu");
        clsTable.exportDataToDropDownList(ref cmbParent, "LableVI", "ID_MN", " parent = 0");
        clsTable.free();
    }
    protected void nhapMenuCon()
    {
        clsTableAction clsTable = new clsTableAction("Menu");
        clsTable.exportDataToGridview(ref GridViewMenu,
            "*", " parent = " + cmbParent.SelectedValue );
        clsTable.free();
    }
    protected void cmbParent_SelectedIndexChanged(object sender, EventArgs e)
    {
        nhapMenuCon();
    }
    protected void butAdd_Click(object sender, EventArgs e)
    {
        if (txtLableVi.Text.Length == 0)
        {
            txtLableVi.BackColor = Color.Red;
            txtLableVi.Focus();
        }
        else
        {
            clsTableAction clsTable = new clsTableAction("Menu");
            string sql = "select max(id_mn) from menu";
            int id = Convert.ToInt32(clsTable.runReturnSQL(sql)) + 1;

            sql = string.Format("insert into menu values ('{0}','{1}',N'{2}')",
                id.ToString(), (chkParent.Checked)? "0" : cmbParent.SelectedValue, txtLableVi.Text);
            //Tien hanh them
            if (clsTable.runSQL(sql))
            {
                txtLableVi.BackColor = Color.White;
                txtLableVi.Focus();
                txtLableVi.Text = "";
                nhapMenuCon();
            }

            clsTable.free();
        }
    }    

    protected void GridViewMenu_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridViewMenu.EditIndex = e.NewEditIndex;
        nhapMenuCon();
    }
    protected void GridViewMenu_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridViewMenu.EditIndex =-1;
        nhapMenuCon();
    }
    protected void GridViewMenu_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int index = GridViewMenu.EditIndex;
        string id = GridViewMenu.Rows[index].Cells[0].Text;
        TextBox txt = (TextBox) GridViewMenu.Rows[index].Cells[1].Controls[0];
        string lableVi = txt.Text;

        //Tien hanh update
        string sql = string.Format("update menu set lableVi=N'{0}' where id_mn = '{1}'", lableVi, id);

        clsTableAction clsTable = new clsTableAction("Menu");
        if (clsTable.runSQL(sql))
        {
            GridViewMenu.EditIndex = -1;
            nhapMenuCon();
        }
        clsTable.free();
    }
    protected void butXoa_Click(object sender, EventArgs e)
    {
        clsQueryList clsQuery = new clsQueryList();
        clsTableAction clsTable = new clsTableAction("Menu");
        for (int i = 0; i < GridViewMenu.Rows.Count; i++)
        { 
            CheckBox chk = (CheckBox) GridViewMenu.Rows[i].Cells[3].FindControl("chkXoa");
            if (chk.Checked)
            {
                string id = GridViewMenu.Rows[i].Cells[0].Text;
                //Tien hanh xoa
                string sql = string.Format("delete from menu where id_mn='{0}'", id);
                clsQuery.addSQL(sql);                
            }
        }
        if (clsQuery.Execute())
        {
            nhapMenuCon();
        }
        clsQuery.free();
        clsTable.free();
    }
   
}
